package org.dkakunsi.aplikasitoko.form.admin;

import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;

import org.dkakunsi.aplikasitoko.data.entity.Kasir;
import org.dkakunsi.aplikasitoko.data.util.ApplicationDataException;
import org.dkakunsi.aplikasitoko.form.Desktop;
import org.dkakunsi.aplikasitoko.service.DataService;


public final class DataKasir extends FormData {

	private static final long serialVersionUID = 1L;
	
	private String[] kolom = {"ID Kasir", "Nama Kasir", "Password"};
	private Kasir kasir;
	private String sign;

	/**
	 * create frame with no desktop initialized
	 * @param judul
	 */
	public DataKasir(String judul) {
		super(judul);
		setter();
	}

	/**
	 * create frame with desktop initialized
	 * @param judul
	 * @param desktop
	 */
	public DataKasir(String judul, Desktop desktop) {
		super(judul, desktop);
		setter();
	}
	
	private void setter(){
		this.setKasir();
		loadInitialize();
		ds = new DataService();
	}

	@Override
	protected void add() {
		reset();
		this.textField.setEnabled(true);
		this.textField_1.setEnabled(true);
		this.textField_2.setEnabled(true);
		sign = "ADD";
	}

	@Override
	protected void update() {
		sign = "UPDATE";
		this.textField.setEnabled(true);
		this.textField_1.setEnabled(true);
		this.textField_2.setEnabled(true);
	}

	@Override
	protected void save() {
		switch(sign){
		case "ADD" :
			kasir = new Kasir();
			kasir.setKode(textField.getText());
			kasir.setNama(textField_1.getText());
			kasir.setPass(textField_2.getText());

			boolean canAdd = isExistInList(this.lsLoaded, this.kasir, true);

			if(canAdd == true){
				lsAdded.add(kasir);
				load(null, kasir);
			} else {
				JOptionPane.showMessageDialog(this, "Kesalahan pada ID/KODE Kasir", "Duplikasi Entry", JOptionPane.ERROR_MESSAGE);
			}
			break;
		case "UPDATE" :
			kasir.setNama(textField_1.getText());
			if(!this.textField_2.getText().equals("*****")){
				kasir.setPass(textField_2.getText());
			}

			boolean canUpdate = this.isExistInList(this.lsLoaded, this.kasir, false);
			
			if(canUpdate == true){
				boolean existInAdd = this.isExistInList(this.lsAdded, this.kasir, false);
				
				if(existInAdd == true){
					Iterator<Object> itA = lsAdded.iterator();
					while(itA.hasNext()){
						Kasir kasirFound = (Kasir) itA.next();
						if(kasirFound.getKode().equals(kasir.getKode())){
							lsAdded.remove(kasirFound);
							lsAdded.add(kasir);
							load(kasirFound, kasir);
							break;
						}
					}
				} else {
					boolean existInUpdate = this.isExistInList(this.lsUpdated, this.kasir, false);

					if(existInUpdate == true){
						Iterator<Object> itU = lsUpdated.iterator();
						while(itU.hasNext()){
							Kasir kasirFound = (Kasir) itU.next();
							if(kasirFound.getKode().equals(kasir.getKode())){
								lsUpdated.remove(kasirFound);
								lsUpdated.add(kasir);
								load(kasirFound, kasir);
								break;
							}
						}
					} else {
						Iterator<Object> itL = lsLoaded.iterator();
						while(itL.hasNext()){
							Kasir kasirFound = (Kasir) itL.next();
							if(kasirFound.getKode().equals(kasir.getKode())){
								lsUpdated.add(kasir);
								load(kasirFound, kasir);
								break;
							}
						}
					}
				}
			}
			break;
		default :
			break;
	}

	sign = "";
	kasir = null;
	reset();
	}

	@Override
	protected void delete() {
		boolean canDelete = this.isExistInList(this.lsLoaded, kasir, false);
		
		if(canDelete == true){
			boolean existInAdd = this.isExistInList(this.lsAdded, kasir, false);
			
			if(existInAdd == true){
				Iterator<Object> itA = lsAdded.iterator();
				while(itA.hasNext()){
					Kasir kasirFound = (Kasir) itA.next();
					if(kasirFound.getKode().equals(kasir.getKode())){
						lsAdded.remove(kasirFound);
						load(kasirFound, null);
						break;
					}
				}
			} else {
				boolean existInUpdate = this.isExistInList(this.lsUpdated, kasir, false);
				
				if(existInUpdate == true){
					Iterator<Object> itU = lsUpdated.iterator();
					while(itU.hasNext()){
						Kasir kasirFound = (Kasir) itU.next();
						if(kasirFound.getKode().equals(kasir.getKode())){
							lsUpdated.remove(kasirFound);
							load(kasirFound, null);
							break;
						}
					}
				} else {
					lsDeleted.add(kasir);
					load(kasir, null);
				}
			}
		}
	}

	@Override
	protected void reset() {
		sign = "";
		this.textField.setText("");
		this.textField_1.setText("");
		this.textField_2.setText("");
		this.textField_3.setText("");
		this.textField_4.setText("");

		this.textField.setEnabled(true);
		this.textField_1.setEnabled(false);
		this.textField_2.setEnabled(false);

		this.textField.requestFocus();
	}

	@Override
	protected void search(String id) throws ApplicationDataException {
		boolean found = false;
		String idKasir = this.textField.getText();
		Iterator<Object> itL = lsLoaded.iterator();

		while(itL.hasNext()){
			kasir = (Kasir) itL.next();
			
			if(kasir.getKode().equals(idKasir)){
				found = true;
				break;
			}
		}

		if(found == true){
			this.textField.setText(kasir.getKode());
			this.textField_1.setText(kasir.getNama());
			this.textField_2.setText("*****");
		} else {
			throw new ApplicationDataException( "Kesalahan KODE" );
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void loadInitialize() {
		try {
			lsLoaded = (List<Object>) ds.getAll("KASIR");
			loadView();
		} catch (ApplicationDataException e) {
			System.out.println(e.getMessage() + "\n@ DataBarang.load()");
		}
	}

	@Override
	protected void loadView() {
		this.setTableModel(kolom);

		Iterator<Object> itL = lsLoaded.iterator();
		while(itL.hasNext()){
			Kasir kasir = (Kasir) itL.next();
			String pswd = null;
			if(kasir.getPass().length() > 0){
				pswd = "*****";
			} else {
				pswd = "";
			}

			try {
				String[] data = { kasir.getKode(), kasir.getNama(), pswd };
				tabModel.addRow(data);
			} catch (NullPointerException exc){
				//do nothing
			} catch (ClassCastException exc){
				JOptionPane.showMessageDialog(this, "ClassCastException @DataKasir.loadView()", "Debugging", JOptionPane.DEFAULT_OPTION);
			}
		}
	}

}
